package ai.xiaodao.tool;

import ai.xiaodao.config.DBConfig;
import ai.xiaodao.db.DBStore;

import java.io.*;
import java.sql.Connection;

/**
 * initialize database before
 */

public class DBInit {
    public static void main(String[] args) {
        String cnfDir = "D:\\proj\\futuapij\\autotrade\\src\\main\\resources\\conf";
        //initialization broker config
        ConfigUtil.initConfig(cnfDir);

        DBStore dbStore = DBStore.getInstance();
        Connection conn = dbStore.getConnection();
        File file = new File(DBConfig.getDBScriptPath());

        BufferedReader br = null;
        String line = "";
        StringBuilder sql = new StringBuilder();
        try {
            br = new BufferedReader(new FileReader(file));

            while ((line = br.readLine()) != null) {
                sql.append(line);
            }

            if (sql.length() > 1) {
                dbStore.exesql(sql.toString(), conn);
            }
        } catch (FileNotFoundException e) {
            System.out.println("database init script not found "+e.getMessage());
        } catch (IOException e) {
            System.out.println("io exception " + e.getMessage());
        }
        System.out.println("database init finished");


    }
}
